package be.android.forap.gui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

public class SmsActivity extends Activity{
	
	private static final String TAG = "SmsActivity";
	private final String SEND = "SMS_SEND";
	private final String DELIVERED = "SMS_DELIVERED";
	PendingIntent sendPI;        
    PendingIntent deliveredPI;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		    	
        sendPI = PendingIntent.getBroadcast(this, 0, new Intent(SEND), 0);        
        deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
        
		Bundle b = getIntent().getExtras();
		if(b != null){
			String phoneNb = b.getString("phoneNumber");
			String mes = b.getString("message");
			Log.i(TAG,"Sms sending...");
			sendSMS(phoneNb, mes);
			Log.i(TAG,"Sms send");
			finish();
		} else {
			Toast.makeText(getBaseContext(), "There was a problem sending the sms", Toast.LENGTH_SHORT).show();
			finish();
		}
		
    }
	
	
	
	
	//---sends a SMS message to another device---
    private void sendSMS(String phoneNumber, String message){      
    	String SENT = "SMS_SENT";
    	String DELIVERED = "SMS_DELIVERED";
    	
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
        
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
    	
        //---when the SMS has been sent---
        BroadcastReceiver b1 = new BroadcastReceiver(){
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode())
				{
				    case Activity.RESULT_OK:
					    Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
					    break;
				    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					    Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
					    break;
				    case SmsManager.RESULT_ERROR_NO_SERVICE:
					    Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
					    break;
				    case SmsManager.RESULT_ERROR_NULL_PDU:
					    Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
					    break;
				    case SmsManager.RESULT_ERROR_RADIO_OFF:
					    Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
					    break;
				}
			}
        };
        registerReceiver(b1, new IntentFilter(SENT));
        
        //---when the SMS has been delivered---
       BroadcastReceiver b2 = new BroadcastReceiver(){
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode())
				{
				    case Activity.RESULT_OK:
					    Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
					    break;
				    case Activity.RESULT_CANCELED:
					    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
					    break;					    
				}
			}
        };
        registerReceiver(b2, new IntentFilter(DELIVERED));        
    	
        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> parts = sms.divideMessage(message);
        Log.i(TAG,"size"+parts.size());
        ArrayList<PendingIntent> sendPIs = new ArrayList<PendingIntent>();        
        ArrayList<PendingIntent> deliveredPIs = new ArrayList<PendingIntent>();
        for(int i = 0;i < parts.size();i++){
        	sendPIs.add(sendPI);
            deliveredPIs.add(deliveredPI);
        }
        Log.i(TAG,"size"+sendPIs.size());
        Log.i(TAG,"size"+deliveredPIs.size());
        sms.sendMultipartTextMessage(phoneNumber, null,  sms.divideMessage(message), sendPIs, deliveredPIs); 
        
        unregisterReceiver(b1);
        unregisterReceiver(b2);
    }    
}
